package com.example.dianpinghook;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.koushikdutta.async.http.NameValuePair;
import com.koushikdutta.async.http.body.JSONObjectBody;
import com.koushikdutta.async.http.body.MultipartFormDataBody;
import com.koushikdutta.async.http.body.StringBody;
import com.koushikdutta.async.http.body.UrlEncodedFormBody;
import com.koushikdutta.async.http.server.AsyncHttpServer;
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
import com.koushikdutta.async.http.server.HttpServerRequestCallback;

import org.json.JSONObject;

import java.net.InetSocketAddress;
import java.util.HashMap;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;

public class dianpingDecode implements IXposedHookLoadPackage {
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
//        XposedBridge.log("Loaded app: " + lpparam.packageName);
        if (!lpparam.packageName.equals("com.dianping.v1")) {
            return;
        }
            XposedBridge.log("+++Loaded app: " + lpparam.packageName);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    AsyncHttpServer server = new AsyncHttpServer();
                    server.post("/test", new HttpServerRequestCallback() {
                        @Override
                        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                            //TODO 可以这里面调用APP的东西了
                            XposedBridge.log(request.getBody().toString()+":bbbbbbb");
                            JSONObject jsonObject = new JSONObject();
                            try {
                                JSONObject json = new JSONObject();
                                if (request.getBody() instanceof UrlEncodedFormBody) {
                                    UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
                                    for (NameValuePair pair : body.get()) {
                                        json.put(pair.getName(), pair.getValue());
                                    }
                                } else if (request.getBody() instanceof JSONObjectBody) {
                                    json = ((JSONObjectBody) request.getBody()).get();
                                } else if (request.getBody() instanceof StringBody) {
                                    json.put("foo", ((StringBody) request.getBody()).get());
                                } else if (request.getBody() instanceof MultipartFormDataBody) {
                                    MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
                                    for (NameValuePair pair : body.get()) {
                                        json.put(pair.getName(), pair.getValue());
                                    }
                                }
                                response.send(json);
                            } catch (Exception e) {
                            }
                        }
                    });


                    server.listen(12346);
                    XposedBridge.log("AsyncHttpServer start");
                }
            }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                AsyncHttpServer server = new AsyncHttpServer();
                    server.get("/test/(.*)", new HttpServerRequestCallback() {  //如果是get
//
                    @Override
                    public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                        //TODO 可以这里面调用APP的东西了
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("status", true);
                            jsonObject.put("message", request.getMatcher().group(1));

                            response.send(jsonObject);
                        } catch (Exception eee) {

                        }
                    }
                });


                server.listen(12345);
                XposedBridge.log("AsyncHttpServer start");
            }
        }).start();

//        hook某个方法
//        findAndHookMethod("com.android.systemui.statusbar.policy.Clock", lpparam.classLoader, "updateClock", new XC_MethodHook() {
//            @Override
//            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                // this will be called before the clock was updated by the original method
//            }
//            @Override
//            protected void afterHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable {
//                // this will be called after the clock was updated by the original method
//            }
//        });
//        Class cla = XposedHelpers.findClass("com.dianping.dataservice.mapi.impl.e", lpparam.classLoader);
//        Object com = XposedHelpers.callStaticMethod(cla, "a");
//        XposedHelpers.callMethod(com, "setDebug", true); // 传入指定值

    }
}
